package com.shxc.smarttrade.old.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.MarketMsgDetailBean;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/7/22.
 */

public class MarketDetailActivity extends BaseActivity {
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_sorce_time)
    TextView tvSorceTime;
    @BindView(R.id.tv_content)
    TextView tvContent;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    MarketMsgDetailBean msb = (MarketMsgDetailBean) msg.obj;
                    MarketMsgDetailBean.DataBean db = msb.getData();

                    if (msb.getErr_code().equals("0")) {
                        if (msb.getData() != null) {
                            if (!TextUtils.isEmpty(db.getTitle()))
                                tvTitle.setText(db.getTitle());
                            if (!TextUtils.isEmpty(db.getSource()))
                                tvSorceTime.setText(db.getSource() + "  " + BaseUtils.getDateFromMillionSeconds(db.getTime()));
                            if (!TextUtils.isEmpty(db.getContent()))
                                tvContent.setText(db.getContent());
                        }

                    } else {
                        showToast(msb.getErr_msg());
                    }

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market_detail);
        initView();
        showShortDialog();
        initData(getIntent().getIntExtra("msgId", -1));
    }

    private void initView() {
        tvCenter.setText("行情速递");
    }

    private void initData(int msgId) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(getApplicationContext(), "accessToken", ""));
        map.put("msgId", msgId + "");
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.MARKET_DETAIL_ACTIVITY_REALMSG_DETAIL, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                showToast("网络异常");
                dismissShortDialog();
            }

            @Override
            public void success(String response, int id) {
                Gson gson = new Gson();
                MarketMsgDetailBean mb = gson.fromJson(response, MarketMsgDetailBean.class);
                Message msg = Message.obtain();
                msg.what = 0;
                msg.obj = mb;
                handler.sendMessage(msg);
                dismissShortDialog();
            }
        });
    }

    @OnClick(R.id.rl_left)
    public void onViewClicked() {
        finish();
    }
}
